package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.integral.IntegralDto;
import com.java110.dto.integral.PreStoreUserIntegralDto;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.po.integral.IntegralDetailPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userIntegralToCommunityBmoImpl")
public class UserIntegralToCommunityBmoImpl implements IMallCommonApiBmo {

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "link", "未包含手机号");
        Assert.hasKeyAndValue(reqJson, "integral", "未包含赠送积分数");
        Assert.hasKeyAndValue(reqJson, "communityId", "未包含小区ID");
    }

    @Override
    @Java110Transactional
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        IntegralDto integralDto = new IntegralDto();
        integralDto.setLink(reqJson.getString("link"));
        integralDto.setObjType(IntegralDto.OBJ_TYPE_PERSON);
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);

        if (ListUtil.isNull(integralDtos)) {
            throw new IllegalArgumentException("积分账户不存在");
        }

        int amount = Integer.parseInt(integralDtos.get(0).getAmount());
        int integral = reqJson.getIntValue("integral");
        if (integral > amount) {
            throw new IllegalArgumentException("积分不够");
        }


        integralDto = new IntegralDto();
        integralDto.setObjId(reqJson.getString("communityId"));
        integralDto.setObjType(IntegralDto.OBJ_TYPE_STORE);
        List<IntegralDto> communityIntegrals = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);

        if (ListUtil.isNull(communityIntegrals)) {
            throw new IllegalArgumentException("小区未开通积分账户");
        }

        //todo 业主账户扣款
        IntegralDetailPo integralDetailPo = new IntegralDetailPo();
        integralDetailPo.setIntegralId(integralDtos.get(0).getIntegralId());
        integralDetailPo.setAmount(integral + "");
        integralDetailPo.setRemark("业主缴费扣款");
        integralV1InnerServiceSMOImpl.withholdIntegral(integralDetailPo);


        //todo 小区账户 预存
        PreStoreUserIntegralDto preStoreUserIntegralDto = new PreStoreUserIntegralDto();
        preStoreUserIntegralDto.setIntegralId(communityIntegrals.get(0).getIntegralId());
        preStoreUserIntegralDto.setAmount(integral);
        preStoreUserIntegralDto.setOrderId(reqJson.getString("orderId"));
        preStoreUserIntegralDto.setRemark(reqJson.getString("remark"));
        integralV1InnerServiceSMOImpl.preStoreUserIntegral(preStoreUserIntegralDto);

    }
}
